--- i26_flower_sack_data.lua
--- Description: 处理背包中特殊物品的效果
--- Author: 旅行者
--- Date: 2025/3/2 4:53

local i26_flower_sack_data = Class(function(self, inst)
    self.inst = inst
    self.owner = nil
    self.data = {
        yellowstaff = {
            active = false -- 唤星，提供大范围发光效果
        },
        yellowamulet = {
            active = false -- 魔光护符，+0.2移速
        },
        ruinshat = {
            active = false -- 铥矿头，+80防御
        },
        raincoat = {
            active = false -- 雨衣，+60防水
        },
        armordreadstone = {
            active = false -- 绝望石盔甲，+10 位面防御
        },
        cane = {
            active = false -- 步行手杖， +0.2移速
        },
        ruins_bat = {
            active = false -- 铥矿棒， +0.2移速
        },
    }
    self.light = nil -- 用于存储当前的光源实例
    self.materials = {
        "yellowstaff",
        "yellowamulet",
        "ruinshat",
        "raincoat",
        "armordreadstone",
        "cane",
        "ruins_bat",
    }
end)
-- 检查背包是否可以接受特定物品
function i26_flower_sack_data:CanAcceptItem(item)
    if TUNING.I26_FLOWER_SACK_ENABLED ~= "enabled" then
        return false
    end
    if not item or not item:IsValid() or not item.prefab then
        return false
    end
    local prefab = item.prefab
    -- 如果物品不在数据表中，或者已经激活，则不能接受
    if self.data[prefab] == nil then
        return false
    end
    if self.data[prefab].active then
        return false
    end
    return true
end

-- 更新物品数据并检查是否需要添加效果
function i26_flower_sack_data:UpdateData(item)
    if TUNING.I26_FLOWER_SACK_ENABLED ~= "enabled" then
        return false
    end
    if not item or not item:IsValid() or not item.prefab then
        return false
    end
    local prefab = item.prefab
    -- 如果物品存在且未激活，则激活并检查效果
    if self.data[prefab] ~= nil and not self.data[prefab].active then
        self.data[prefab].active = true
        if (self.owner ~= nil) then
            self:CheckAddGlowEffect(self.owner)
        end
        item:Remove()
        return true
    end
    return false
end

-- 检查并添加发光效果或速度加成
function i26_flower_sack_data:CheckAddGlowEffect(owner)
    if TUNING.I26_FLOWER_SACK_ENABLED ~= "enabled" then
        return
    end
    -- 使用安全访问方式，确保字段存在
    local yellowstaffActive = self.data.yellowstaff and self.data.yellowstaff.active or false
    local yellowamuletActive = self.data.yellowamulet and self.data.yellowamulet.active or false
    local ruinshatActive = self.data.ruinshat and self.data.ruinshat.active or false
    local raincoatActive = self.data.raincoat and self.data.raincoat.active or false
    local armordreadstoneActive = self.data.armordreadstone and self.data.armordreadstone.active or false
    local caneActive = self.data.cane and self.data.cane.active or false
    local ruinsBatActive = self.data.ruins_bat and self.data.ruins_bat.active or false
    self:RemoveGlowEffect(owner) -- 移除之前的光效

    -- 如果唤星激活，添加大范围白光
    if yellowstaffActive then
        self:AddGlowEffect(owner, { 1, 1, 1 }, 6) -- 大范围白光
    elseif yellowamuletActive then
        self:AddGlowEffect(owner, { 1, 1, 0.5 }, 3) -- 小范围黄光
    end
    local initSpeed = 0.6 -- 初始速度-40%
    -- 如果魔光护符激活，添加小范围黄光和速度加成
    if yellowamuletActive then
        -- 魔光护符 +0.2移速
        initSpeed = initSpeed + 0.2
    end
    if caneActive then
        -- 步行手杖 +0.2移速
        initSpeed = initSpeed + 0.2
    end
    if ruinsBatActive then
        -- 铥矿棒 +0.2移速
        initSpeed = initSpeed + 0.2
    end
    if owner.components.equippable then
        owner.components.equippable.walkspeedmult = initSpeed
    end
    -- 铥矿头
    if ruinshatActive then
        owner:AddComponent("armor") -- 护甲
        if owner.components.armor then
            owner.components.armor:InitIndestructible(.80) -- 增加80%属性
        end
    end

    if raincoatActive then
        -- 雨衣
        owner:AddComponent("waterproofer") -- 防水
        if owner.components.waterproofer then
            owner.components.waterproofer:SetEffectiveness(.60) -- 60 防水
        end
    end

    if armordreadstoneActive then
        -- 绝望石盔甲
        owner:AddComponent("planardefense") -- 位面防御
        if owner.components.planardefense then
            owner.components.planardefense:SetBaseDefense(10) -- 位面防御
        end
    end
end

-- 添加发光效果
function i26_flower_sack_data:AddGlowEffect(inst, color, range)
    -- 移除现有光源
    if self.light ~= nil then
        self.light:Remove()
        self.light = nil
    end
    -- 生成新的光源
    self.light = SpawnPrefab("minerhatlight")
    if self.light then
        self.light.entity:SetParent(inst.entity)
        -- 设置光源的颜色和范围
        if color and range then
            local r, g, b = unpack(color)
            self.light.Light:SetRadius(range)
            self.light.Light:SetColour(r, g, b)
        end
        -- 定义光源更新函数，根据时间变化启用或禁用光源
        local function UpdateLight()
            if self.light ~= nil and self.light:IsValid() then
                if TheWorld.state.isnight then
                    self.light.Light:Enable(true)
                else
                    self.light.Light:Enable(false)
                end
            end
        end
        UpdateLight() -- 初始化时更新光源状态
        -- 监听世界时间变化事件
        inst:WatchWorldState("isnight", UpdateLight)
        inst:WatchWorldState("isdusk", UpdateLight)
        inst:WatchWorldState("isday", UpdateLight)
    end
end

-- 移除发光效果
function i26_flower_sack_data:RemoveGlowEffect(inst)
    if self.light ~= nil then
        self.light:Remove()
        self.light = nil
    end
end

function i26_flower_sack_data:EquipFn(owner)
    self.owner = owner
    self:CheckAddGlowEffect(owner)
end

function i26_flower_sack_data:UnEquipFn(owner)
    self.owner = nil
    self:Remove(owner)
end

-- 移除所有效果
function i26_flower_sack_data:Remove(inst)
    self:RemoveGlowEffect(inst)
end

-- 保存数据
function i26_flower_sack_data:OnSave()
    return {
        data = self.data
    }
end

-- 加载数据
function i26_flower_sack_data:OnLoad(data)
    if data and data.data then
        self.data = data.data
    end
    -- 确保所有材料都被初始化
    for _, material in ipairs(self.materials) do
        if self.data[material] == nil then
            self.data[material] = { active = false }
        end
    end
end

return i26_flower_sack_data